package com.example.android.softkeyboard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class SuggestionsList {
	private List<String> currentSuggestions;
	private String[] frequentSymbols;
	private HashMap<Integer, List<String>> suggestionsCache;
	
	private boolean symbolsShown;
	final int maxCurrentSuggestionsCnt = 10;
	
	public SuggestionsList() {
		currentSuggestions = new ArrayList<String>();
		frequentSymbols = new String[5];
		suggestionsCache = new HashMap<Integer, List<String>>();
	}
	
    public interface SuggestionsUpdateListener {
    	public void onSuggestionsChange(List<String> suggestions);
    }  
	
	private SuggestionsUpdateListener mListener = null;
	public void setSuggestionsUpdateListener (SuggestionsUpdateListener listener) {
		this.mListener = listener;
	}
	
	public void setSuggestions(List<String> suggestions) {
		symbolsShown = false;
		int wordsHash = Arrays.hashCode(SoftKeyboard.mWords);
		List<String> tempSuggestions = new ArrayList<String>();
		if (suggestionsCache.containsKey(wordsHash)) {		
			tempSuggestions = suggestionsCache.get(wordsHash);
			for (String s : suggestions) {
				if (!tempSuggestions.contains(s)) {
					tempSuggestions.add(s);
				}
			}
		} else {
			tempSuggestions = suggestions;
		}
		suggestionsCache.put(wordsHash, tempSuggestions);
		
		updateCurrentSuggestions();	
		this.mListener.onSuggestionsChange(currentSuggestions);
	}
	
	public void showSymbols() {
		symbolsShown = true;
		currentSuggestions = getSymbols();
		this.mListener.onSuggestionsChange(addSpaces(currentSuggestions));
	}
	
	public List<String> getSuggestions() {
		return this.currentSuggestions;
	}
	
	public boolean isEmpty() {
		if (currentSuggestions == null || currentSuggestions.isEmpty() == true) {
			return true;
		}else {
			return false;
		}
	}
	
	public String findByIndex(int index) {
		if (index < currentSuggestions.size()) {
			return currentSuggestions.get(index);
		} else {
			return "";
		}
	}
	
	public int numberOfElements() {		
		return this.currentSuggestions.size();
	}
	
	public void updateCurrentSuggestions() {
		if (!symbolsShown) {
			String letters = SoftKeyboard.mLetters;
			int wordsHash = Arrays.hashCode(SoftKeyboard.mWords);
			List<String> tempSuggestions = new ArrayList<String>();
			if (suggestionsCache.containsKey(wordsHash)) {
				tempSuggestions = suggestionsCache.get(wordsHash);
			}			
			List<String> sugs = new ArrayList<String>();
			int i = 0;
			if (!symbolsShown && tempSuggestions.size() > 0) {
				for (String s: tempSuggestions) {
					if (i >= maxCurrentSuggestionsCnt) {
						break;
					}
					if (s.startsWith(letters) && s.length() > letters.length()) {
						sugs.add(s);
						i++;
					}
				}
			}
			currentSuggestions = sugs;
			//if (currentSuggestions.size() < 10) {
//					ServerConnector.lastResultsEmpty = true;
//		}
			setUpdate();
		}
	}
	
	public void setUpdate() {
		this.mListener.onSuggestionsChange(currentSuggestions);
	}
	
	public void setSymbols(String[] symbols) {
		frequentSymbols = symbols;
	}
	
	public List<String> getSymbols() {
		List<String> result = new ArrayList<String>();
		for (String s : frequentSymbols) {
			result.add(s);
		}
		return result;
	}
	
	public boolean symbolsShown() {
		return symbolsShown;
	}
	
	public void setSymbolsShown(boolean symbols) {
		this.symbolsShown = symbols;
	}
	
	public void clearCache() {
		suggestionsCache.clear();
		currentSuggestions.clear();
		setUpdate();
	}
	
	//workaround for too narrow Symbols, replace with better
	//view structure
	public List<String> addSpaces(List<String> list) {
		List<String> result = new ArrayList<String>();
		for (String s : list) {
			result.add("   " + s + "   ");
		}
		return result;
	}
	
}
